import jwx from '@/common/jwx'
import config from "@/config/index.js"
export default function(callback) {
	let promise;
	if (typeof callback !== "function") {
		promise = new Promise((resolve, reject) => {
			callback = function(resolvedata, rejectdata) {
				if (resolvedata) {
					// 都是gcj02坐标转换为百度坐标
					let wz = uni.$z.gps.gcj02tobd09(resolvedata.lng, resolvedata.lat);

					resolve({
						lng: wz[0],
						lat: wz[1]
					})
				} else {
					reject(rejectdata);
				}
			};
		});
	}
	//#ifndef H5
	uni.getLocation({
		type: 'wgs84',
		success: function(res) {
			let wz = uni.$z.gps.wgs84togcj02(res.longitude, res.latitude)

			callback({
				lng: wz[0],
				lat: wz[1]
			})
		},
		fail: function() {
			callback(false)
		}
	});
	//#endif
	// #ifdef H5
	if (config.islocalhost) {
		let wz = uni.$z.gps.wgs84togcj02(120.122726, 30.327005)
		callback({
			lng: wz[0],
			lat: wz[1]
		})

		// uni.getLocation({
		// 	type: "wgs84",
		// 	success: function(res) {
		// 		callback({
		// 			lng: res.longitude,
		// 			lat: res.latitude,
		// 		});
		// 		console.log("当前位置的经度：" + res.longitude);
		// 		console.log("当前位置的纬度：" + res.latitude);
		// 	},
		// });;
	} else if (uni.$z.IsWeixinOrAlipay() == "weixin" && jwx.isWechat()) {
		jwx.getlocation(function(res) {
			let position = {
				lng: res.longitude,
				lat: res.latitude
			};
			//alert(res.longitude+","+res.latitude)
			if (!res.longitude || !res.latitude) {
				position = {
					lng: 0,
					lat: 0
				}
			}
			callback(position)
		})
	} else {

		let geolocation = new qq.maps.Geolocation("J7GBZ-NPOWO-IJVWT-SNE7B-BTW2V-7KB5G", "电瓶车防盗系统");
		let options = {
			timeout: 20000
		};
		geolocation.getLocation(function(e) {
			callback(e);
		}, function() {

		}, options);

	}

	// #endif

	return promise;
}